package com.ItTallents.Maria.Nikola.Ivan.cardsgame;

import gameLogic.Login;
import gameLogic.UserSqlHelper;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class LogInActivity extends Activity implements OnClickListener {

	private EditText passwordField;
	private EditText usernameField;

	private Button registerButton;
	private Button loginButton;

	private Login login;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_log_in);

        passwordField = (EditText) findViewById(R.id.passwordInputFeld);
		usernameField = (EditText) findViewById(R.id.usernameInputFeld);

		registerButton = (Button) findViewById(R.id.registerButton);
		loginButton = (Button) findViewById(R.id.loginButton);

		UserSqlHelper helper = new UserSqlHelper(this);

		login = Login.getInstance(null, helper);

		loginButton.setOnClickListener(this);
		registerButton.setOnClickListener(this);
		
//		Star kod:
//		
//		registerButton.setOnClickListener(new OnClickListener() {
//
//			@Override
//			public void onClick(View v) {
//
//				if (login.registerUser(usernameField.getText().toString(),
//						passwordField.getText().toString())) {
//					Intent intent = new Intent(MainActivity.this,
//							UserActivity.class);
//					MainActivity.this.startActivity(intent);
//				}else{
//					usernameField.setText("already eists");
//				}
//
//			}
//		});
//
//		loginButton.setOnClickListener(new OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				if(login.login(usernameField.getText().toString(), passwordField.getText().toString()))
//				{
//					Intent intent = new Intent(MainActivity.this,
//							UserActivity.class);
//					MainActivity.this.startActivity(intent);
//				}
//			}
//		});
	}

//	Nov kod:
	
	public void onClick(View v) {
		final int id = v.getId();
		
		if (id == loginButton.getId()) {
			if(login.login(usernameField.getText().toString(), passwordField.getText().toString())) {
				Intent intent = new Intent(LogInActivity.this, MenuActivity.class);
				LogInActivity.this.startActivity(intent);
			}
		}
		
		if (id == registerButton.getId()) {
			if (login.registerUser(usernameField.getText().toString(), passwordField.getText().toString())) {
				Intent intent = new Intent(LogInActivity.this, UserActivity.class);
				LogInActivity.this.startActivity(intent);
			} else {
				usernameField.setText("already eists");
			}
		}
	}
	
}
